<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="form.id?'编辑部门':'新增部门'" :visible="showDialog" @close="closeDialog">
    <el-form ref="deptRef" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item prop="code" label="部门编码">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item prop="manager" label="部门负责人">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in emList" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" size="small" @click="addPartItem">确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getemployeeSimple } from '@/api/employee.js'
import { addDepartsments, getDepartDetail, updateDepartments } from '@/api/departments.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentdata: {
      type: Object,
      default: () => ({})
    },
    beforetransform: {
      type: Array,
      default: () => []
    }
  },
  data() {
    const codeValidate = (rule, value, callback) => {
      let flag
      if (this.form.id) {
        flag = this.beforetransform.some(item => item.code === value && value !== this.currentdata.code)
      } else {
        flag = this.beforetransform.some(item => item.code === value)
      }
      if (flag) {
        callback(new Error('编码重复，请重新输入'))
      } else {
        callback()
      }
    }
    return {
      emList: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: codeValidate, trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getemployee()
    console.log(this.currentdate)
  },
  methods: {
    // 关闭弹窗
    closeDialog() {
      this.$emit('close')
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      this.$refs.deptRef.resetFields()
    },
    // 获取下拉菜单用户
    async getemployee() {
      const res = await getemployeeSimple()
      this.emList = res
    },
    async getEditData(id) {
      const detail = await getDepartDetail(id)
      console.log('dangqian', detail)
      this.form = detail
    },
    // 添加操作
    addPartItem() {
      this.$refs.deptRef.validate(async(isOK) => {
        if (isOK) {
          if (this.form.id) {
            await updateDepartments(this.form)
          } else {
            await addDepartsments({ ...this.form, pid: this.currentdata.id || '' })
          }
          // 提交，调用接口
          this.closeDialog()
          this.$emit('updateList')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
